package com.project.erp.service.crm.company.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.project.erp.common.utils.Page;
import com.project.erp.dao.crm.company.CompanyDao;
import com.project.erp.model.crm.company.Company;
import com.project.erp.service.crm.company.CompanyService;

@Service
@Transactional
public class CompanyServiceImpl implements CompanyService {

	@Autowired
	private CompanyDao  companyDao;
	
	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Page<Company> getCompanyPageBySearch(Page<Company> page,
			Company company) {
		return companyDao.getCompanyPageBySearch(page, company);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void saveCompany(Company company) {
		companyDao.save(company);
	}

	@Transactional(propagation=Propagation.NOT_SUPPORTED,readOnly=true)
	@Override
	public Company getCompanyById(Long companyId) {
		return companyDao.get(companyId);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void editCompany(Company company) {
		companyDao.save(company);
	}

	@Transactional(rollbackFor=Exception.class)
	@Override
	public void delCompany(String[] companyIds) {
		if(null == companyIds || companyIds.length == 0){
			return;
		}
		List<Long> ids = new ArrayList<Long>();
		for (String companyId : companyIds) {
			ids.add(Long.valueOf(companyId));
		}
		//删除主表信息
		companyDao.deleteByIdsList(ids);
	}

	@Override
	public Company getDefaultCompany() {
		return companyDao.getDefaultCompany();
	}

}
